// functionPage/pages/diaryNews/diaryNews.js
import Http from '../../../utils/http.js';
import { getTimePart } from '../../../utils/util.js';
import { giveLikeApi, submitComment } from '../../../utils/tools.js';
const http = new Http();
const app = getApp();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        isIphone: false,
        attentionNum: 0,
        imageArr: [
            'https://static.map8.com/sp/20200128/0_171106558099.jpg',
            'https://static.map8.com/sp/20200128/0_171106464704.jpg',
            'https://static.map8.com/sp/20200128/0_171106500357.jpg',
            'https://static.map8.com/sp/20200128/0_172344455687.jpg',
        ],
        //日记操作弹框
        othersBtnShow: false,
        operateIndex: Number,
        diaryArr: [],
        // 日记分页加载
        pageNum: 0,
        loadingDiary: false,
        loadingAllDiary: true,
        remarkValue: '', //评论
        remarkBox: '',
        remarkId: '',
        remarkEdit: false,
        anonymous: false, //评论匿名
    },
    attentionListTap() {
        wx.navigateTo({
            url: './attentionList/attentionList',
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;
        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
        _that.getConcernUserDiaryList();
        _that.getminePageNum();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    // 日记本操作
    //显示弹出层
    operateBtn(e) {
        this.setData({
            othersBtnShow: true,
            operateIndex: e.currentTarget.dataset.index,
        });
    },
    //显示弹出层遮罩层
    operateHideBtn(e) {
        this.setData({
            othersBtnShow: false,
        });
    },
    //点赞
    supportBtn(e) {
        let _that = this;
        let flg = e.target.dataset.praiseflg == '00' ? '01' : '00';
        _that.data.diaryArr[e.target.dataset.index].praiseflg = flg;
        _that.setData({
            othersBtnShow: false,
            diaryArr: _that.data.diaryArr,
        });
        giveLikeApi('13', e.target.dataset.id, flg);
        wx.showToast({
            title: flg == '01' ? '点赞成功' : '已取消点赞',
            icon: 'none', //如果要纯文本，不要icon，将值设为'none'
            duration: 1000,
        });
    },
    // 隐藏匿名
    removeFocus() {
        this.setData({
            remarkEdit: false,
            remarkBox: false,
        });
    },
    // 切换匿名
    toggle() {
        this.setData({
            anonymous: !this.data.anonymous,
        });
    },
    remarkBtn(e) {
        this.setData({
            othersBtnShow: false,
            remarkBox: true,
            remarkEdit: true,
            remarkId: e.target.dataset.id,
        });
    },
    //评论
    remarkInput(e) {
        this.setData({
            remarkValue: e.detail.value,
        });
    },
    remarkBlur() {
        this.setData({
            remarkBox: false,
            remarkEdit: false,
        });
    },
    //提交评论
    submitComment() {
        let _that = this;
        if (!_that.data.remarkValue) return;
        submitComment(
            '',
            _that.data.remarkId,
            '13',
            '00',
            _that.data.remarkValue,
            _that.data.anonymous ? '01' : '00'
        ).then((res) => {
            if (res.status == 0) {
                _that.setData({
                    remarkValue: '',
                });
            }
        });
    },
    //获取关注人数量
    getminePageNum(e) {
        let _that = this;
        wx.request({
            url: app.globalData.rootPath + `cjMap/personalPageNum`,
            method: 'GET',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
            },
            data: {
                authId: app.globalData.authId,
            },
            success: function (res) {
                console.log(res.data);
                _that.setData({
                    attentionNum: res.data.data.concernnum,
                });
            },
        });
    },
    // 获取所有关注人的日记
    getConcernUserDiaryList() {
        if (!this.loadingAllDiary) {
            this.setData({
                pageNum: this.data.pageNum + 1,
                loadingDiary: true,
                loadingAllDiary: false,
            });
            let data = {
                authId: app.globalData.authId,
                pageNo: this.data.pageNum,
                pageSize: 20,
            };
            http.getRequest('cjMap/getConcernUserDiaryList', data).then(
                (res) => {
                    if (res.status == 0) {
                        res.data.map((item) => {
                            item.createtime = getTimePart(
                                item.createtime
                            ).format;
                            this.data.diaryArr.push(item);
                        });
                        this.setData({
                            diaryArr: this.data.diaryArr,
                            loadingDiary: false,
                        });

                        if (res.pager.currentPage >= res.pager.totalPages) {
                            this.setData({
                                loadingAllDiary: true,
                            });
                        }
                    } else {
                        this.setData({
                            loadingAllDiary: true,
                            loadingDiary: false,
                        });
                    }
                }
            );
        }
    },
});
